package com.romotc.qqrobot.webqq.qqInterfaceLog;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.romotc.qqrobot.bean.QQLogedin;
import com.romotc.qqrobot.config.bean.QQUrlWatcherConfig;
import com.romotc.qqrobot.webqq.bean.Poll2_ResponseBody;
import com.romotc.qqrobot.webqq.bean.Poll2_ResponseBody_Result;
import com.romotc.qqrobot.webqq.bean.Poll2_ResponseBody_Result_Value;
import com.romotc.qqrobot.webqq.qqInterfaceCall.Get_file2_InterCall;
import com.romotc.qqrobot.webqq.qqInterfaceCall.dataProcessor.Get_file2_processor;
import com.romotc.scache.RequestSpy;
/**
 * 自动接收文件
 * @author Administrator
 *
 */
public class Poll2_FileAutoRecive_Log extends Poll2_Processor_Log {

	
	@Override
	public boolean checkSkipLog(RequestSpy rqSpy, QQUrlWatcherConfig config) {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	 * 防止接受两遍文件
	 */
	private static Map<Long, Poll2_ResponseBody_Result_Value> transFiles 
		= new HashMap<Long, Poll2_ResponseBody_Result_Value>();
	public synchronized static Poll2_ResponseBody_Result_Value getTransFile(long sessionId) {
		return transFiles.get(sessionId);
	}
	public synchronized static void setTransFile(long sessionId, Poll2_ResponseBody_Result_Value fileMsg) {
		transFiles.put(Long.valueOf(sessionId), fileMsg);
	}
	
	
	@Override
	void processPollEmpty(RequestSpy rqSpy, QQUrlWatcherConfig config,
			Poll2_ResponseBody response) {
		// TODO Auto-generated method stub
		
	}

	@Override
	void processPollResult(RequestSpy rqSpy, QQUrlWatcherConfig config,
			Poll2_ResponseBody_Result result, Poll2_ResponseBody response,
			int resultIdx) {
		if("recv".equals(result.getValue().getMode()) 
				&& getTransFile(result.getValue().getSession_id()) == null) {
			
			//文件接受
			JSONObject reqDataObj = new JSONObject(); 
			Get_file2_InterCall getfileCall = Get_file2_InterCall.getInstance();
			try {
				reqDataObj.put(getfileCall.Key_Must_Input_FileName, result.getValue().getName());
				reqDataObj.put(getfileCall.Key_Must_Input_Lcid, result.getValue().getSession_id());
				reqDataObj.put(getfileCall.Key_Must_Input_SenderUin, result.getValue().getFrom_uin());
				
				setTransFile(result.getValue().getSession_id(), result.getValue());
				getfileCall.call(QQLogedin.getCurQqLogedIn(rqSpy), 
						reqDataObj, Get_file2_processor.getInstance());
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		}
	}

	@Override
	String getPollType() {
		return Poll2_Processor_Log.Key_File_message;
	}

}
